/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.planeteVelo.bean;

import com.planeteVelo.backedBean.Client;
import com.planeteVelo.backedBean.LigneTicket;
import com.planeteVelo.backedBean.Ticket;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;

import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

/**
 *
 * @author apo
 */
@Named
@RequestScoped
public class TicketBean {

    private Ticket ticket = new Ticket();
    private List<LigneTicket> lesLignes = new ArrayList();
    @PersistenceContext
    EntityManager em;
    @Resource
    UserTransaction tx;
    
       @PostConstruct
    public void init() {

        Query query = em.createQuery("SELECT l FROM LigneTicket l");
        lesLignes = query.getResultList();

    }
      public void ajouterTicket() {


        FacesMessage message = null;

        message = new FacesMessage("Ajout validé");
        //ajouter le message JSF
        FacesContext.getCurrentInstance().addMessage(null, message);
        try {
            tx.begin();
            em.persist(this.ticket);
            tx.commit();
            em.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        init();
    }
public void modifierClient(Client c) {
        FacesMessage message = null;
    message = new FacesMessage("Modification validée");
    //ajouter le message JSF
    FacesContext.getCurrentInstance().addMessage(null, message);
        try {
            tx.begin();
            em.persist(em.merge(c));
            tx.commit();
            
        }
         catch(Exception ex)
                {
                System.out.println(ex);
                }
    }

public void supprimerClient(Client c) {
    FacesMessage message = null;
    message = new FacesMessage("Suppression validée");
    //ajouter le message JSF
    FacesContext.getCurrentInstance().addMessage(null, message);
        try {
            tx.begin();
            em.remove(em.merge(c));
            tx.commit();
            em.close();
        }
         catch(Exception ex)
                {
                System.out.println(ex);
                }
      init();
}

    public void setTicket(Ticket ticket) {
        this.ticket = ticket;
    }

    public void setLesLignes(List<LigneTicket> lesLignes) {
        this.lesLignes = lesLignes;
    }

    public Ticket getTicket() {
        return ticket;
    }

    public List<LigneTicket> getLesLignes() {
        return lesLignes;
    }

}
